use Thomson_Buhaug_Urdal_Rosvold_2021_Data, clear
set scheme plotplain
set more off
set matsize 800


***********************************
* Create plot of unrest over time *
***********************************

* By Region *
replace REGION="Asia" if country=="China"
sort date REGION
by date: egen tot_unrest_asia = total(nevents) if REGION=="Asia"
label var tot_unrest_asia "Asia"
by date: egen tot_unrest_la = total(nevents) if REGION=="Latin America"
label var tot_unrest_la "Latin America"
by date: egen tot_unrest_africa = total(nevents) if REGION=="Sub-Saharan Africa"
label var tot_unrest_africa "Africa"
by date: egen tot_unrest_me = total(nevents) if REGION=="Middle East and North Africa"
label var tot_unrest_me "Middle East/N Africa"

twoway (lpoly tot_unrest_asia date, lpattern(solid) lwidth(medium) lcolor(gs6)) ///
(lpoly tot_unrest_la date, lpattern(dash) lwidth(medthick) lcolor(gs9)) ///
(lpoly tot_unrest_africa date, lpattern(shortdash) lwidth(medthick) lcolor(lightgray)) ///
(lpoly tot_unrest_me date, lpattern(solid) lwidth(medium) lcolor(black) xsize(6) ysize(5) title("") ytitle("No. Events (Monthly)") ///
xtitle("")xlabel(0 "1960" 120 "1970" 240 "1980" 360 "1990" 480 "2000" 600 "2010" 660 "2015") graphregion(color(white)) bgcolor(white) ///
legend(pos(6) cols(2) order(1 "Asia" 2 "L America" 3 "Africa" 4 "ME/N Africa") size(small)))
*graph export Figure_1.eps, replace


************************
* ANALYSIS FOR TABLE 1 *
************************

*******************************
* H1.1 - Effects of elections *
*******************************
sort id_unpop date
* Model 1.1
nbreg nevents elect_any c.polity2##c.polity2 ///
ln_citypop ln_gdp_gw coup_any conflict recession ///
L1.polity2 L1.ln_citypop L1.ln_gdp_gw L1.coup_any L1.conflict L1.recession ///
i.id_unpop i.year, robust
est store elect

* Effects of control variables
*margins, at(polity2=(-10(1)10) elect_any=(0)) post
*margins, dydx(coup_any) at(elect_any=(0)) post
*margins, dydx(L.coup_any) at(elect_any=(0)) post
*margins, dydx(conflict) at(elect_any=(0)) post
*margins, at(ln_citypop=(6.050808 8.552568) elect_any=(0)) post


* Create summary stats table *
summ nevents org_events unorg_events bin_unrest bin_unrest_org bin_unrest_unorg ///
polity2 elect_any ///
coup_any conflict recession ///
ln_citypop ln_gdp_gw if e(sample)

* Model 1.2: Organized
nbreg org_events elect_any c.polity2##c.polity2 ///
ln_citypop ln_gdp_gw coup_any conflict recession ///
L1.polity2 L1.ln_citypop L1.ln_gdp_gw L1.coup_any L1.conflict L1.recession ///
i.id_unpop i.year, robust
est store elect_org

* Model 1.3: Unorganized
nbreg unorg_events elect_any c.polity2##c.polity2 ///
ln_citypop ln_gdp_gw coup_any conflict recession ///
L1.polity2 L1.ln_citypop L1.ln_gdp_gw L1.coup_any L1.conflict L1.recession ///
i.id_unpop i.year, robust
est store elect_unorg


*****************************
* H1.2 & 1.3 - Leads & Lags *
*****************************

* Model 1.4: All Events
nbreg nevents elect_any ///
F1.elect_any F2.elect_any F3.elect_any ///
L1.elect_any L2.elect_any L3.elect_any ///
c.polity2##c.polity2 ///
ln_citypop ln_gdp_gw coup_any conflict recession ///
L1.polity2 L1.ln_citypop L1.ln_gdp_gw L1.coup_any L1.conflict L1.recession ///
i.id_unpop i.year, robust
est store elect_leads_lags


** [Robustness Test: All Events w/o Repression]
nbreg nevents_nr elect_any ///
F1.elect_any F2.elect_any F3.elect_any ///
L1.elect_any L2.elect_any L3.elect_any ///
c.polity2##c.polity2 ///
ln_citypop ln_gdp_gw coup_any conflict recession ///
L1.polity2 L1.ln_citypop L1.ln_gdp_gw L1.coup_any L1.conflict L1.recession ///
i.id_unpop i.year, robust


* Model 1.5: Organized Events
nbreg org_events elect_any ///
F1.elect_any F2.elect_any F3.elect_any ///
L1.elect_any L2.elect_any L3.elect_any ///
c.polity2##c.polity2 ///
ln_citypop ln_gdp_gw coup_any conflict recession ///
L1.polity2 L1.ln_citypop L1.ln_gdp_gw L1.coup_any L1.conflict L1.recession ///
i.id_unpop i.year, robust
est store org_leads_lags
margins, dydx(F3.elect_any F2.elect_any F.elect_any elect_any L.elect_any L2.elect_any L3.elect_any) post
est store org_leads_lags_margins

* Figure 2: Coefplot
coefplot org_leads_lags, vertical yline(0) ///
keep(F3.elect_any F2.elect_any F.elect_any ///
elect_any L.elect_any L2.elect_any L3.elect_any) ///
order(F3.elect_any F2.elect_any F.elect_any ///
elect_any L.elect_any L2.elect_any L3.elect_any) ///
coeflabels(F.elect_any = "m-1" ///
F2.elect_any = "m-2"  ///
F3.elect_any = "m-3"  ///
elect_any = "m"  ///
L.elect_any = "m+1" ///
L2.elect_any = "m+2"  ///
L3.elect_any = "m+3", labsize(small)) msymbol(Oh) ///
title("Organized Disturbances") ytitle("Effect of Election") xtitle("Month vs Election") ///
graphregion(color(white)) bgcolor(white) aspect(1)
graph save org_leads_lags, replace

* Robustness Test: Organized Events w/o Repression
nbreg org_events_nr elect_any ///
F1.elect_any F2.elect_any F3.elect_any ///
L1.elect_any L2.elect_any L3.elect_any ///
c.polity2##c.polity2 ///
ln_citypop ln_gdp_gw coup_any conflict recession ///
L1.polity2 L1.ln_citypop L1.ln_gdp_gw L1.coup_any L1.conflict L1.recession ///
i.id_unpop i.year, robust


* Model 1.6: Unorganized Events
nbreg unorg_events elect_any ///
F1.elect_any F2.elect_any F3.elect_any ///
L1.elect_any L2.elect_any L3.elect_any ///
c.polity2##c.polity2 ///
ln_citypop ln_gdp_gw coup_any conflict recession ///
L1.polity2 L1.ln_citypop L1.ln_gdp_gw L1.coup_any L1.conflict L1.recession ///
i.id_unpop i.year, robust
est store unorg_leads_lags
margins, dydx(F3.elect_any F2.elect_any F.elect_any elect_any L.elect_any L2.elect_any L3.elect_any) post
est store unorg_leads_lags_margins

* Figure 2: Coefplot
coefplot unorg_leads_lags_margins, vertical yline(0) ///
keep(F3.elect_any F2.elect_any F.elect_any ///
elect_any L.elect_any L2.elect_any L3.elect_any) ///
order(F3.elect_any F2.elect_any F.elect_any ///
elect_any L.elect_any L2.elect_any L3.elect_any) ///
coeflabels(F.elect_any = "m-1" ///
F2.elect_any = "m-2"  ///
F3.elect_any = "m-3"  ///
elect_any = "m"  ///
L.elect_any = "m+1" ///
L2.elect_any = "m+2"  ///
L3.elect_any = "m+3", labsize(small)) msymbol(Oh) ///
title("Unorganized Disturbances") ytitle("Effect of Election") xtitle("Month vs Election") ///
graphregion(color(white)) bgcolor(white) aspect(1)
graph save unorg_leads_lags, replace

graph combine org_leads_lags.gph unorg_leads_lags.gph, ///
cols(2) graphregion(color(white) margin(zero))
*graph export Figure_2.eps, replace

*********
* SUREG *
*********
* Models 1.7, 1.8: Organized and Unorganized together
sort id_unpop date
sureg (org_events elect_any ///
F1.elect_any F2.elect_any F3.elect_any ///
L1.elect_any L2.elect_any L3.elect_any ///
c.polity2##c.polity2 ///
ln_citypop ln_gdp_gw coup_any conflict recession ///
L1.polity2 L1.ln_citypop L1.ln_gdp_gw L1.coup_any L1.conflict L1.recession ///
i.id_unpop i.year) ///
(unorg_events elect_any ///
F1.elect_any F2.elect_any F3.elect_any ///
L1.elect_any L2.elect_any L3.elect_any ///
c.polity2##c.polity2 ///
ln_citypop ln_gdp_gw coup_any conflict recession ///
L1.polity2 L1.ln_citypop L1.ln_gdp_gw L1.coup_any L1.conflict L1.recession ///
i.id_unpop i.year)

*Test coefficients are different
test [org_events]F2.elect_any = [unorg_events]F2.elect_any, mtest(bonferroni)
test [org_events]F1.elect_any = [unorg_events]F1.elect_any, mtest(bonferroni)
test [org_events]elect_any = [unorg_events]elect_any, mtest(bonferroni)
test [org_events]L1.elect_any = [unorg_events]L1.elect_any, mtest(bonferroni) 
test [org_events]L2.elect_any = [unorg_events]L2.elect_any, mtest(bonferroni)



* Results Table 1
esttab elect elect_org elect_unorg elect_leads_lags org_leads_lags unorg_leads_lags, ///
keep(elect_any ///
F.elect_any F2.elect_any F3.elect_any ///
L.elect_any L2.elect_any L3.elect_any ///
polity2 c.polity2#c.polity2 ln_citypop ln_gdp_gw coup_any conflict) ///
order(elect_any ///
F.elect_any F2.elect_any F3.elect_any ///
L.elect_any L2.elect_any L3.elect_any ///
polity2 c.polity2#c.polity2 ln_citypop ln_gdp_gw coup_any conflict) ///
mtitle("All Evs" "Org Evs" "Unorg Evs" "All Evs" "Org Evs" "Unorg Evs" "All Evs" "Org Evs" "Unorg Evs") ///
label b(2) se star(* 0.10 ** 0.05 *** 0.01) aic compress nogaps ///
addnotes("All models with city and year fixed effects, controls for City Population, GDP/Capita, Recessions." ///
"Analysis years 1960-2011. All models with robust standard errors.")

* Full table for appendix
esttab elect elect_org elect_unorg elect_leads_lags org_leads_lags unorg_leads_lags, ///
keep(F.elect_any F2.elect_any F3.elect_any ///
L.elect_any L2.elect_any L3.elect_any ///
polity2 c.polity2#c.polity2 coup_any conflict ///
ln_citypop ln_gdp_gw recession ///
L.polity2 L.ln_citypop L.ln_gdp_gw L.coup_any L.conflict L.recession) ///
order(elect_any ///
F.elect_any F2.elect_any F3.elect_any ///
L.elect_any L2.elect_any L3.elect_any ///
polity2 c.polity2#c.polity2 coup_any conflict ///
ln_citypop ln_gdp_gw recession ///
L.polity2 L.ln_citypop L.ln_gdp_gw L.coup_any L.conflict L.recession) ///
mtitle("All Evs" "Org Evs" "Unorg Evs" "All Evs" "Org Evs" "Unorg Evs" "All Evs" "Org Evs" "Unorg Evs") ///
label b(2) se star(* 0.10 ** 0.05 *** 0.01) aic compress nogaps ///
addnotes("All models with robust standard errors." "Analysis years 1960-2011.")



***************************************************************************************************************************************************************
************************
* ANALYSIS FOR TABLE 2 *
************************

****************************************
* Interact elections with Polity score *
****************************************
* Model 2.1: Linear Polity Score
nbreg nevents i.elect_any##c.polity2 ///
ln_citypop ln_gdp_gw coup_any conflict recession ///
L1.elect_any L1.polity2 L1.ln_citypop L1.ln_gdp_gw L1.coup_any L1.conflict L1.recession ///
i.id_unpop i.year, robust
est store polity
*margins, dydx(i.elect_any) at(c.polity2=(-10(1)10))

*Model 2.2: Organized Events
nbreg org_events i.elect_any##c.polity2 ///
ln_citypop ln_gdp_gw coup_any conflict recession ///
L1.elect_any L1.polity2 L1.ln_citypop L1.ln_gdp_gw L1.coup_any L1.conflict L1.recession ///
i.id_unpop i.year, robust
est store polity_org
*margins, dydx(i.elect_any) at(c.polity2=(-10(1)10))

*Model 2.3: Unorganized Events
nbreg unorg_events i.elect_any##c.polity2 ///
ln_citypop ln_gdp_gw coup_any conflict recession ///
L1.elect_any L1.polity2 L1.ln_citypop L1.ln_gdp_gw L1.coup_any L1.conflict L1.recession ///
i.id_unpop i.year, robust
est store polity_unorg
*margins, dydx(i.elect_any) at(c.polity2=(-10(1)10))


************************
* Polity Score squared *
************************
* Model 2.4: All Events
nbreg nevents i.elect_any##c.polity2##c.polity2 ///
ln_citypop ln_gdp_gw coup_any conflict recession ///
L1.elect_any L1.polity2 L1.ln_citypop L1.ln_gdp_gw L1.coup_any L1.conflict L1.recession ///
i.id_unpop i.year, robust
est store polity_sq
*margins, dydx(i.elect_any) at(c.polity2=(-10(1)10))
*margins elect_any, at(c.polity2=(-10(1)10))
* Control variables
*margins, at(polity2=(-10(1)10)) post
*margins, at(polity2=(-10(1)10) elect_any=(0)) post


* Model 2.5: Organized Events
nbreg org_events i.elect_any##c.polity2##c.polity2 ///
ln_citypop ln_gdp_gw coup_any conflict recession ///
L1.elect_any L1.polity2 L1.ln_citypop L1.ln_gdp_gw L1.coup_any L1.conflict L1.recession ///
i.id_unpop i.year, robust
est store polity_sq_org
margins, dydx(i.elect_any) at(c.polity2=(-10(1)10))
marginsplot, recast(line) recastci(rline) plot1opts(lwidth(medium)) ciopts(lwidth(thin) lpattern(longdash)) ///
title("") ytitle("Effect of Election") xtitle("Polity") graphregion(color(white)) bgcolor(white) ///
addplot(kdensity polity2 if e(sample) & elect_any==1, lpattern(dot) yaxis(2) xlabel(-10(2)10)) yline(0, lwidth(thin)) ///
legend(off) aspect(1)
graph save polity_sq_org, replace

margins elect_any, at(c.polity2=(-10(1)10))
marginsplot, recast(line) recastci(rline) plot1opts(lwidth(medium)) ci1opts(lwidth(thin) lpattern(longdash)) ///
plot2opts(lwidth(medthick) lpattern(dash)) ci2opts(lwidth(medthin) lpattern(longdash)) ///
title("") ytitle("Events") xtitle("Polity") graphregion(color(white)) bgcolor(white) ///
legend(pos(6) order(3 "No Elect" 4 "Elect" 5 "Polity") cols(3) size(small)) aspect(1) ///
addplot(kdensity polity2 if e(sample) & elect_any==1, lpattern(dot) yaxis(2) xlabel(-10(2)10))
graph save polity_sq_org_evs, replace

graph combine polity_sq_org.gph polity_sq_org_evs.gph, cols(2) ///
title("Organized Disturbances") graphregion(color(white))
*graph save polity_org_comb, replace


** Robustness test: Organized Events w/o Repression
nbreg org_events_nr i.elect_any##c.polity2##c.polity2 ///
ln_citypop ln_gdp_gw coup_any conflict recession ///
L1.elect_any L1.polity2 L1.ln_citypop L1.ln_gdp_gw L1.coup_any L1.conflict L1.recession ///
i.id_unpop i.year, robust
*margins, dydx(i.elect_any) at(c.polity2=(-10(1)10))



* Model 2.6: Unorganized Events
nbreg unorg_events i.elect_any##c.polity2##c.polity2 ///
ln_citypop ln_gdp_gw coup_any conflict recession ///
L1.elect_any L1.polity2 L1.ln_citypop L1.ln_gdp_gw L1.coup_any L1.conflict L1.recession ///
i.id_unpop i.year, robust
est store polity_sq_unorg
margins, dydx(i.elect_any) at(c.polity2=(-10(1)10))
marginsplot, recast(line) recastci(rline) plot1opts(lwidth(medium)) ciopts(lwidth(thin) lpattern(longdash)) ///
title("") ytitle("Effect of Election") xtitle("Polity") graphregion(color(white)) bgcolor(white) ///
addplot(kdensity polity2 if e(sample) & elect_any==1, lpattern(dot) yaxis(2) xlabel(-10(2)10)) yline(0, lwidth(thin)) ///
legend(off) aspect(1)
graph save polity_sq_unorg, replace

margins elect_any, at(c.polity2=(-10(1)10))
marginsplot, recast(line) recastci(rline) plot1opts(lwidth(medium)) ci1opts(lwidth(thin) lpattern(longdash)) ///
plot2opts(lwidth(medthick) lpattern(dash)) ci2opts(lwidth(medthin) lpattern(longdash)) ///
title("") ytitle("Events") xtitle("Polity") graphregion(color(white)) bgcolor(white) ///
legend(pos(6) order(3 "No Elect" 4 "Elect" 5 "Polity") cols(3) size(small)) aspect(1) ///
addplot(kdensity polity2 if e(sample) & elect_any==1, lpattern(dot) yaxis(2) xlabel(-10(2)10))
graph save polity_sq_unorg_evs, replace

graph combine polity_sq_unorg.gph polity_sq_unorg_evs.gph, cols(2) ///
title("Unorganized Disturbances") graphregion(color(white))
*graph save polity_unorg_comb, replace

graph combine polity_org_comb.gph polity_unorg_comb.gph, cols(1) xsize(8) ysize(9)
graph export Figure_3.eps, replace

*********
* SUREG *
*********
* Models 2.7, 2.8: SUREG
*** Level of Democracy for Table 2
sureg (org_events i.elect_any##c.polity2##c.polity2 ///
L1.elect_any ///
ln_citypop ln_gdp_gw coup_any conflict recession ///
L1.polity2 L1.ln_citypop L1.ln_gdp_gw L1.coup_any L1.conflict L1.recession ///
i.id_unpop i.year) ///
(unorg_events i.elect_any##c.polity2##c.polity2 ///
L1.elect_any ///
ln_citypop ln_gdp_gw coup_any conflict recession ///
L1.polity2 L1.ln_citypop L1.ln_gdp_gw L1.coup_any L1.conflict L1.recession ///
i.id_unpop i.year)

*Figure A1: 
margins elect_any, at(c.polity2=(-10(1)10))
marginsplot, recastci(rline) ///
plot1opts(msymbol(dh) lwidth(medium)) ci1opts(lwidth(thin) lpattern(longdash)) ///
plot3opts(msymbol(d) lwidth(medium) lpattern(dash)) ci3opts(lwidth(thin) lpattern(longdash)) ///
plot2opts(msymbol(th) lwidth(medium)) ci2opts(lwidth(thin) lpattern(longdash)) ///
plot4opts(msymbol(t) lwidth(medium) lpattern(dash)) ci4opts(lwidth(thin) lpattern(longdash)) ///
title("Models 2.7 & 2.8") ytitle("Events") xtitle("Polity") graphregion(color(white)) bgcolor(white) ///
addplot(kdensity polity2 if e(sample) & elect_any==1, lpattern(dot) yaxis(2) xlabel(-10(2)10)) ///
legend(pos(6) cols(2) size(small)) aspect(1)

* Results Table 2
esttab polity polity_org polity_unorg polity_sq polity_sq_org polity_sq_unorg, ///
keep(1.elect_any polity2 c.polity2#c.polity2 ///
1.elect_any#c.polity2 1.elect_any#c.polity2#c.polity2 ///
L.elect_any ln_citypop ln_gdp_gw coup_any conflict) ///
order(1.elect_any polity2 c.polity2#c.polity2 ///
1.elect_any#c.polity2 1.elect_any#c.polity2#c.polity2 ///
L.elect_any ln_citypop ln_gdp_gw coup_any conflict) ///
mtitle("All Evs" "Org Evs" "Unorg Evs" "All Evs" "Org Evs" "Unorg Evs" "All Evs (Exec)" "All Evs (Parl)") ///
label b(2) se star(* 0.10 ** 0.05 *** 0.01) aic compress nogaps ///
addnotes("All models with city and year fixed effects, controls for City Population, GDP/Capita, Recessions." ///
"Analysis years 1960-2011. All models with robust standard errors.")


* Full table for appendix
esttab polity polity_org polity_unorg polity_sq polity_sq_org polity_sq_unorg, ///
keep(1.elect_any polity2 c.polity2#c.polity2 ///
1.elect_any#c.polity2 1.elect_any#c.polity2#c.polity2 ///
L.elect_any ln_citypop ln_gdp_gw coup_any conflict ///
L.polity2 L.ln_citypop L.ln_gdp_gw L.coup_any L.conflict L.recession) ///
order(1.elect_any polity2 c.polity2#c.polity2 ///
1.elect_any#c.polity2 1.elect_any#c.polity2#c.polity2 ///
L.elect_any ln_citypop ln_gdp_gw coup_any conflict) ///
mtitle("All Evs" "Org Evs" "Unorg Evs" "All Evs" "Org Evs" "Unorg Evs" "All Evs (Exec)" "All Evs (Parl)") ///
label b(2) se star(* 0.10 ** 0.05 *** 0.01) aic compress nogaps longtable ///
addnotes("All models with robust standard errors." "Analysis years 1960-2011.") ///
title("Full Results, Democracy, Elections and Urban Disturbances")



***************************************************************************************************************************************************************
********************
* ROBUSTNESS TESTS *
********************

**************
* Using XPolity Instead of Polity *

* generate xpolity
replace xconst=1 if xconst==0
gen xpolity = (xrcomp + xropen + xconst) - 7
label var xpolity "XPolity"
*** TABLE 2 - interaction election x xpolity
* 2.1x
nbreg nevents i.elect_any##c.xpolity ///
ln_citypop ln_gdp_gw coup_any conflict recession ///
L1.elect_any L1.xpolity L1.ln_citypop L1.ln_gdp_gw L1.coup_any L1.conflict L1.recession ///
i.id_unpop i.year, robust nolog
est store xpolity

* 2.2x
nbreg org_events i.elect_any##c.xpolity ///
ln_citypop ln_gdp_gw coup_any conflict recession ///
L1.elect_any L1.xpolity L1.ln_citypop L1.ln_gdp_gw L1.coup_any L1.conflict L1.recession ///
i.id_unpop i.year, robust nolog
est store xpolity_org

* 2.3x
nbreg unorg_events i.elect_any##c.xpolity ///
ln_citypop ln_gdp_gw coup_any conflict recession ///
L1.elect_any L1.xpolity L1.ln_citypop L1.ln_gdp_gw L1.coup_any L1.conflict L1.recession ///
i.id_unpop i.year, robust nolog
est store xpolity_unorg

*** Quadratic XPolity Term
* 2.4x All Events
nbreg nevents i.elect_any##c.xpolity##c.xpolity ///
ln_citypop ln_gdp_gw coup_any conflict recession ///
L1.elect_any L1.xpolity L1.ln_citypop L1.ln_gdp_gw L1.coup_any L1.conflict L1.recession ///
i.id_unpop i.year, robust nolog
est store xpolity_sq

* 2.5x Organized Events
nbreg org_events i.elect_any##c.xpolity##c.xpolity ///
ln_citypop ln_gdp_gw coup_any conflict recession ///
L1.elect_any L1.xpolity L1.ln_citypop L1.ln_gdp_gw L1.coup_any L1.conflict L1.recession ///
i.id_unpop i.year, robust nolog
est store xpolity_sq_org

margins, dydx(i.elect_any) at(c.xpolity=(-6(1)7))
marginsplot, recast(line) recastci(rline) plot1opts(lwidth(medium)) ciopts(lwidth(thin) lpattern(longdash)) ///
title("") ytitle("Effect of Election") xtitle("XPolity") graphregion(color(white)) bgcolor(white) ///
addplot(kdensity xpolity if e(sample) & elect_any==1, lpattern(dot) yaxis(2) xlabel(-6(2)7) yline(0, lwidth(thin))) ///
legend(off) aspect(1)

margins elect_any, at(xpolity=(-6(1)7))
marginsplot, recast(line) recastci(rline) plot1opts(lwidth(medium)) ci1opts(lwidth(thin) lpattern(longdash)) ///
plot2opts(lwidth(medthick) lpattern(dash)) ci2opts(lwidth(medthin) lpattern(longdash)) ///
title("") ytitle("Events") xtitle("XPolity") graphregion(color(white)) bgcolor(white) ///
legend(pos(6) order(3 "No Elect" 4 "Elect" 5 "XPolity") cols(3) size(small)) aspect(1) ///
addplot(kdensity xpolity if e(sample) & elect_any==1, lpattern(dot) yaxis(2) xlabel(-6(2)7))

* 2.6x Unorganized Events
nbreg unorg_events i.elect_any##c.xpolity##c.xpolity ///
ln_citypop ln_gdp_gw coup_any conflict recession ///
L1.elect_any L1.xpolity L1.ln_citypop L1.ln_gdp_gw L1.coup_any L1.conflict L1.recession ///
i.id_unpop i.year, robust nolog
est store xpolity_sq_unorg

margins, dydx(i.elect_any) at(c.xpolity=(-6(1)7))
marginsplot, recast(line) recastci(rline) plot1opts(lwidth(medium)) ciopts(lwidth(thin) lpattern(longdash)) ///
title("") ytitle("Effect of Election") xtitle("XPolity") graphregion(color(white)) bgcolor(white) ///
addplot(kdensity xpolity if e(sample) & elect_any==1, lpattern(dot) yaxis(2) xlabel(-6(2)7) yline(0, lwidth(thin))) ///
legend(off) aspect(1)

margins elect_any, at(xpolity=(-6(1)7))
marginsplot, recast(line) recastci(rline) plot1opts(lwidth(medium)) ci1opts(lwidth(thin) lpattern(longdash)) ///
plot2opts(lwidth(medthick) lpattern(dash)) ci2opts(lwidth(medthin) lpattern(longdash)) ///
title("") ytitle("Events") xtitle("XPolity") graphregion(color(white)) bgcolor(white) ///
legend(pos(6) order(3 "No Elect" 4 "Elect" 5 "XPolity") cols(3) size(small)) aspect(1) ///
addplot(kdensity xpolity if e(sample) & elect_any==1, lpattern(dot) yaxis(2) xlabel(-6(2)7))

* Results Table 2
esttab xpolity xpolity_org xpolity_unorg xpolity_sq xpolity_sq_org xpolity_sq_unorg, ///
keep(1.elect_any xpolity c.xpolity#c.xpolity ///
1.elect_any#c.xpolity 1.elect_any#c.xpolity#c.xpolity ///
L.elect_any ln_citypop ln_gdp_gw coup_any conflict) ///
order(1.elect_any xpolity c.xpolity#c.xpolity ///
1.elect_any#c.xpolity 1.elect_any#c.xpolity#c.xpolity ///
L.elect_any ln_citypop ln_gdp_gw coup_any conflict) ///
mtitle("All Evs" "Org Evs" "Unorg Evs" "All Evs" "Org Evs" "Unorg Evs") ///
label b(2) se star(* 0.10 ** 0.05 *** 0.01) aic compress nogaps ///
addnotes("All models with city and year fixed effects, controls for City Population, GDP/Capita, Recessions." ///
"Analysis years 1960-2011. All models with robust standard errors.")



******************************************
* Using VDem Polyarchy Instead of Polity *
******************************************
* Linear VDem Polyarchy
nbreg nevents i.elect_any##c.v2x_polyarchy ///
ln_citypop ln_gdp_gw coup_any conflict recession ///
L1.elect_any L1.polity2 L1.ln_citypop L1.ln_gdp_gw L1.coup_any L1.conflict L1.recession ///
i.id_unpop i.year, robust
est store vdem

*Organized Events
nbreg org_events i.elect_any##c.v2x_polyarchy ///
ln_citypop ln_gdp_gw coup_any conflict recession ///
L1.elect_any L1.polity2 L1.ln_citypop L1.ln_gdp_gw L1.coup_any L1.conflict L1.recession ///
i.id_unpop i.year, robust
est store vdem_org

*Unorganized Events
nbreg unorg_events i.elect_any##c.v2x_polyarchy ///
ln_citypop ln_gdp_gw coup_any conflict recession ///
L1.elect_any L1.polity2 L1.ln_citypop L1.ln_gdp_gw L1.coup_any L1.conflict L1.recession ///
i.id_unpop i.year, robust
est store vdem_unorg

* VDem Polyarchy Squared 
nbreg nevents i.elect_any##c.v2x_polyarchy##c.v2x_polyarchy ///
ln_citypop ln_gdp_gw coup_any conflict recession ///
L1.elect_any L1.polity2 L1.ln_citypop L1.ln_gdp_gw L1.coup_any L1.conflict L1.recession ///
i.id_unpop i.year, robust
est store vdem_sq

*Organized Events
nbreg org_events i.elect_any##c.v2x_polyarchy##c.v2x_polyarchy ///
ln_citypop ln_gdp_gw coup_any conflict recession ///
L1.elect_any L1.polity2 L1.ln_citypop L1.ln_gdp_gw L1.coup_any L1.conflict L1.recession ///
i.id_unpop i.year, robust
est store vdem_sq_org

margins, dydx(i.elect_any) at(c.v2x_polyarchy=(0(0.05)1))
marginsplot, recast(line) recastci(rline) plot1opts(lwidth(medium)) ciopts(lwidth(thin) lpattern(longdash)) ///
title("") ytitle("Effect of Election") xtitle("VDem Polyarchy") graphregion(color(white)) bgcolor(white) ///
addplot(kdensity v2x_polyarchy if e(sample) & elect_any==1, lpattern(dot) yaxis(2) xlabel(0(0.25)1)) yline(0, lwidth(thin)) ///
legend(off) aspect(1)

margins elect_any,  at(c.v2x_polyarchy=(0(0.05)1))
marginsplot, recast(line) recastci(rline) plot1opts(lwidth(medium)) ci1opts(lwidth(thin) lpattern(longdash)) ///
plot2opts(lwidth(medthick) lpattern(dash)) ci2opts(lwidth(medthin) lpattern(longdash)) ///
title("") ytitle("Events") xtitle("VDem Polyarchy") graphregion(color(white)) bgcolor(white) ///
legend(pos(6) order(3 "No Elect" 4 "Elect") cols(3) size(small)) aspect(1)

*Unorganized Events
nbreg unorg_events i.elect_any##c.v2x_polyarchy##c.v2x_polyarchy ///
ln_citypop ln_gdp_gw coup_any conflict recession ///
L1.elect_any L1.polity2 L1.ln_citypop L1.ln_gdp_gw L1.coup_any L1.conflict L1.recession ///
i.id_unpop i.year, robust
est store vdem_sq_unorg

margins, dydx(i.elect_any) at(c.v2x_polyarchy=(0(0.05)1))
marginsplot, recast(line) recastci(rline) plot1opts(lwidth(medium)) ciopts(lwidth(thin) lpattern(longdash)) ///
title("") ytitle("Effect of Election") xtitle("VDem Polyarchy") graphregion(color(white)) bgcolor(white) ///
addplot(kdensity v2x_polyarchy if e(sample) & elect_any==1, lpattern(dot) yaxis(2) xlabel(0(0.25)1)) yline(0, lwidth(thin)) ///
legend(off) aspect(1)

margins elect_any,  at(c.v2x_polyarchy=(0(0.05)1))
marginsplot, recast(line) recastci(rline) plot1opts(lwidth(medium)) ci1opts(lwidth(thin) lpattern(longdash)) ///
plot2opts(lwidth(medthick) lpattern(dash)) ci2opts(lwidth(medthin) lpattern(longdash)) ///
title("") ytitle("Events") xtitle("VDem Polyarchy") graphregion(color(white)) bgcolor(white) ///
legend(pos(6) order(3 "No Elect" 4 "Elect") cols(3) size(small)) aspect(1)

* VDem Results Table
esttab vdem vdem_org vdem_unorg vdem_sq vdem_sq_org vdem_sq_unorg, ///
keep(1.elect_any v2x_polyarchy c.v2x_polyarchy#c.v2x_polyarchy ///
1.elect_any#c.v2x_polyarchy 1.elect_any#c.v2x_polyarchy#c.v2x_polyarchy ///
L.elect_any ln_citypop ln_gdp_gw coup_any conflict) ///
order(1.elect_any v2x_polyarchy c.v2x_polyarchy#c.v2x_polyarchy ///
1.elect_any#c.v2x_polyarchy 1.elect_any#c.v2x_polyarchy#c.v2x_polyarchy ///
L.elect_any ln_citypop ln_gdp_gw coup_any conflict) ///
mtitle("All Evs" "Org Evs" "Unorg Evs" "All Evs" "Org Evs" "Unorg Evs" "All Evs (Exec)" "All Evs (Parl)") ///
label b(2) se star(* 0.10 ** 0.05 *** 0.01) aic compress nogaps ///
addnotes("All models with city and year fixed effects, controls for City Population, GDP/Capita, Recessions." ///
"Analysis years 1960-2011. All models with robust standard errors.")


***************************
* Binary Unrest Indicator *
***************************
logit bin_unrest i.elect_any c.polity2##c.polity2 ///
ln_citypop ln_gdp_gw coup_any conflict recession ///
L1.polity2 L1.ln_citypop L1.ln_gdp_gw L1.coup_any L1.conflict L1.recession ///
i.id_unpop i.year, robust
est store elect_bin

logit bin_unrest_org i.elect_any c.polity2##c.polity2 ///
ln_citypop ln_gdp_gw coup_any conflict recession ///
L1.polity2 L1.ln_citypop L1.ln_gdp_gw L1.coup_any L1.conflict L1.recession ///
i.id_unpop i.year, robust
est store elect_org_bin

logit bin_unrest_unorg i.elect_any c.polity2##c.polity2 ///
ln_citypop ln_gdp_gw coup_any conflict recession ///
L1.polity2 L1.ln_citypop L1.ln_gdp_gw L1.coup_any L1.conflict L1.recession ///
i.id_unpop i.year, robust
est store elect_unorg_bin

* Timing
logit bin_unrest i.elect_any ///
F1.elect_any F2.elect_any F3.elect_any ///
L1.elect_any L2.elect_any L3.elect_any ///
c.polity2##c.polity2 ///
ln_citypop ln_gdp_gw coup_any conflict recession ///
L1.polity2 L1.ln_citypop L1.ln_gdp_gw L1.coup_any L1.conflict L1.recession ///
i.id_unpop i.year, robust
est store elect_leads_lags_bin

logit bin_unrest_org i.elect_any ///
F1.elect_any F2.elect_any F3.elect_any ///
L1.elect_any L2.elect_any L3.elect_any ///
c.polity2##c.polity2 ///
ln_citypop ln_gdp_gw coup_any conflict recession ///
L1.polity2 L1.ln_citypop L1.ln_gdp_gw L1.coup_any L1.conflict L1.recession ///
i.id_unpop i.year, robust
est store elect_leads_lags_org_bin


* Linear Polity Score
logit bin_unrest i.elect_any##c.polity2 ///
ln_citypop ln_gdp_gw coup_any conflict recession ///
L1.elect_any L1.polity2 L1.ln_citypop L1.ln_gdp_gw L1.coup_any L1.conflict L1.recession ///
i.id_unpop i.year, robust
est store polity_bin

*Organized Events
logit bin_unrest_org i.elect_any##c.polity2 ///
ln_citypop ln_gdp_gw coup_any conflict recession ///
L1.elect_any L1.polity2 L1.ln_citypop L1.ln_gdp_gw L1.coup_any L1.conflict L1.recession ///
i.id_unpop i.year, robust
est store polity_bin_org

*Unorganized Events
logit bin_unrest_unorg i.elect_any##c.polity2 ///
ln_citypop ln_gdp_gw coup_any conflict recession ///
L1.elect_any L1.polity2 L1.ln_citypop L1.ln_gdp_gw L1.coup_any L1.conflict L1.recession ///
i.id_unpop i.year, robust
est store polity_bin_unorg

** Polity Score squared
* All Events
logit bin_unrest i.elect_any##c.polity2##c.polity2 ///
ln_citypop ln_gdp_gw coup_any conflict recession ///
L1.elect_any L1.polity2 L1.ln_citypop L1.ln_gdp_gw L1.coup_any L1.conflict L1.recession ///
i.id_unpop i.year, robust
est store polity_bin_sq

*Organized Events
logit bin_unrest_org i.elect_any##c.polity2##c.polity2 ///
ln_citypop ln_gdp_gw coup_any conflict recession ///
L1.elect_any L1.polity2 L1.ln_citypop L1.ln_gdp_gw L1.coup_any L1.conflict L1.recession ///
i.id_unpop i.year, robust
est store polity_bin_sq_org
margins, dydx(i.elect_any) at(c.polity2=(-10(1)10))
marginsplot, recast(line) recastci(rline) plot1opts(lwidth(medium)) ciopts(lwidth(thin) lpattern(longdash)) ///
title("") ytitle("Effect of Election") xtitle("Polity") graphregion(color(white)) bgcolor(white) ///
addplot(kdensity polity2 if e(sample) & elect_any==1, lpattern(dot) yaxis(2) xlabel(-10(2)10)) yline(0, lwidth(thin)) ///
legend(off) aspect(1)

margins elect_any, at(c.polity2=(-10(1)10))
marginsplot, recast(line) recastci(rline) plot1opts(lwidth(medium)) ci1opts(lwidth(thin) lpattern(longdash)) ///
plot2opts(lwidth(medthick) lpattern(dash)) ci2opts(lwidth(medthin) lpattern(longdash)) ///
title("") ytitle("Pr(Unrest)") xtitle("Polity") graphregion(color(white)) bgcolor(white) ///
legend(pos(6) order(3 "No Elect" 4 "Elect") cols(3) size(small)) aspect(1)


*Unorganized Events
logit bin_unrest_unorg i.elect_any##c.polity##c.polity ///
ln_citypop ln_gdp_gw coup_any conflict recession ///
L1.elect_any L1.polity2 L1.ln_citypop L1.ln_gdp_gw L1.coup_any L1.conflict L1.recession ///
i.id_unpop i.year, robust
est store polity_bin_sq_unorg
margins, dydx(i.elect_any) at(c.polity2=(-10(1)10))
marginsplot, recast(line) recastci(rline) plot1opts(lwidth(medium)) ciopts(lwidth(thin) lpattern(longdash)) ///
title("") ytitle("Effect of Election") xtitle("Polity") graphregion(color(white)) bgcolor(white) ///
addplot(kdensity polity2 if e(sample) & elect_any==1, lpattern(dot) yaxis(2) xlabel(-10(2)10)) yline(0, lwidth(thin)) ///
legend(off) aspect(1)


margins elect_any, at(c.polity2=(-10(1)10))
marginsplot, recast(line) recastci(rline) plot1opts(lwidth(medium)) ci1opts(lwidth(thin) lpattern(longdash)) ///
plot2opts(lwidth(medthick) lpattern(dash)) ci2opts(lwidth(medthin) lpattern(longdash)) ///
title("") ytitle("Pr(Unrest)") xtitle("Polity") graphregion(color(white)) bgcolor(white) ///
legend(pos(6) order(3 "No Elect" 4 "Elect") cols(3) size(small)) aspect(1)


*** Table of Results for Binary Models
esttab polity_bin polity_bin_org polity_bin_unorg polity_bin_sq polity_bin_sq_org polity_bin_sq_unorg, ///
keep(1.elect_any polity2 c.polity2#c.polity2 ///
1.elect_any#c.polity2 1.elect_any#c.polity2#c.polity2 ///
ln_citypop ln_gdp_gw coup_any conflict recession) ///
order(1.elect_any polity2 c.polity2#c.polity2 ///
1.elect_any#c.polity2 1.elect_any#c.polity2#c.polity2 ///
ln_citypop ln_gdp_gw coup_any conflict recession) ///
mtitle("All Evs (0/1)" "Org Evs (0/1)" "Unorg Evs (0/1)" "All Evs (0/1)" "Org Evs (0/1)" "Unorg Evs (0/1)") ///
label b(2) se star(* 0.10 ** 0.05 *** 0.01) aic compress nogaps


***********************************************
* Create Table of Cities and Totals of Events *
***********************************************
keep if e(sample)
sort city year
by city: egen total_events = total(nevents)
label var total_events "Total Disturbances"
by city: egen total_events_d = total(death)
label var total_events_d "Total Disturbances (L)"
by city: egen total_events_org = total(org_events)
label var total_events_org "Total Org Disturbances"
by city: egen total_events_unorg = total(unorg_events)
label var total_events_unorg "Total Unorg Disturbances"

gen ratio = total_events_org/total_events_unorg
corr ln_gdp ratio

by city: keep if _n==1

keep city country total_events total_events_d total_events_org total_events_unorg

*dataout, save(City_Table.tex) tex replace
